package com.anysoft.visitdd.example;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.Executors;

public class App {
    static final String API_HEAD = "http://192.168.1.105:8080";
    static final String SERVER_PUB_KEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAu9KeWBap4YzhIT1uJ58cokigZWqyxlCQ9lueblsxEvchJmntOaLLF3rrvwdFd7e+0e2QCAx07vLtWK1g/yPl9QVyAH6toJtA+Xtou4CtJ78+8eW/mvRKhwi4IIYHlpgVXEBUYZ/ruCTlNg+tQgSRV2TBZI25x6UZZZmH6a5nSzqcQeL2C+QQfYzmjPFxmrHN0A4HmplKduaBOAAhFg2+7wD6JBYp7CI8UyFn4qPZmVNBIR9MdFw2mpM5YR9GZuy4exl+us9FyJB7E4XxAYCgASLSh/MO/fd4e8iuaY2Sz5cI5Hv7GofbVXaUXMA3oUmEVGqrQGi45fCCJubavMyJOQIDAQAB";
    public static void main(String[] args) throws Exception {
//        var pubKey = getPublicKey();
//        Logger.getGlobal().info(pubKey);

//        var pubKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAu9KeWBap4YzhIT1uJ58cokigZWqyxlCQ9lueblsxEvchJmntOaLLF3rrvwdFd7e+0e2QCAx07vLtWK1g/yPl9QVyAH6toJtA+Xtou4CtJ78+8eW/mvRKhwi4IIYHlpgVXEBUYZ/ruCTlNg+tQgSRV2TBZI25x6UZZZmH6a5nSzqcQeL2C+QQfYzmjPFxmrHN0A4HmplKduaBOAAhFg2+7wD6JBYp7CI8UyFn4qPZmVNBIR9MdFw2mpM5YR9GZuy4exl+us9FyJB7E4XxAYCgASLSh/MO/fd4e8iuaY2Sz5cI5Hv7GofbVXaUXMA3oUmEVGqrQGi45fCCJubavMyJOQIDAQAB";

//        queryRecords(pubKey);

//        var getBody = collectGetBody();
//        getRecord(pubKey, getBody);

//        try (var executor = Executors.newFixedThreadPool(10)) {
//            for (int loop = 0; loop < 1000; loop++) {
//                executor.submit(new Runnable() {
//                    @Override
//                    public void run() {
//                        try {
//                            OwnerClient.addRecords(10);
//                        } catch (Exception e) {
//                            throw new RuntimeException(e);
//                        }
//                    }
//                });
//            }
//        }

//        OwnerClient.queryRecords();
        OwnerClient.changeRecords();

    }


    public static HttpRequest.Builder fillToken(String key) {
        var slat = LocalDate.now().format(DateTimeFormatter.ofPattern("yyMMdd"));
        var hash = ProjectSecurityCommon.hash(key+slat);
//        System.out.println("Hash");
//        System.out.println(hash);
        return HttpRequest.newBuilder()
                .header("token-type","1")
                .header("token-salt", slat)
                .header("token-hash", hash);
    }

    // 获取公钥
    public static String getPublicKey() throws IOException, InterruptedException {
        try (var hc = HttpClient.newBuilder().build()) {
            var req = HttpRequest.newBuilder().GET().uri(URI.create(API_HEAD+"/gateway/public-key")).build();
            var res = hc.send(req, HttpResponse.BodyHandlers.ofString());
            var resBody = res.body();
            var parser = new JsonParser();
            var arm = parser.parse(resBody).getAsJsonObject();
//        Logger.getGlobal().info(arm.toString());
            return arm.get("data").getAsString();
        }
    }

}
